package dao

import (
	context2 "context"
	"elflog/common/context"
	"elflog/component/ndb"
	"elflog/component/nlog"
	"elflog/module/module-account/model"
	"github.com/jinzhu/gorm"
)

type AccountDao struct {
	log *nlog.NLog
	Dao *ndb.NDb
}

func NewAccountDao(log *nlog.NLog, dao *ndb.NDb) *AccountDao {
	dao.GetDb(context.NewContext()).AutoMigrate(new(model.AccountMaster), new(model.AccountLogger))
	return &AccountDao{
		log: log,
		Dao: dao,
	}
}

func (a *AccountDao) GetAccountByName(ctx context2.Context, name string) (account *model.AccountMaster, err error) {
	account = new(model.AccountMaster)
	err = a.Dao.GetDb(ctx).Model(account).First(account, "login_name=?", name).Error
	if err != nil {
		if gorm.IsRecordNotFoundError(err) {
			return nil, nil
		}
		return nil, err
	}
	return account, nil
}
